package org.example.market.controller;

import org.example.market.config.ProviderQuery;
import org.example.market.entity.Provider;

import org.example.market.service.providerService.ProviderService;
import org.example.market.util.PageResult;
import org.example.market.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @PostMapping
    public Result<Provider> addProvider(@RequestBody Provider provider) {
        return providerService.addProvider(provider);
    }

    @GetMapping
    public Result<List<Provider>> getProviderList() {
        return providerService.getAllProviderList();
    }

    @GetMapping("/{id}")
    public Result<Provider> getProviderDetail(@PathVariable int id) {
        return providerService.getProviderDetail(id);
    }

    @PutMapping
    public Result<Provider> updateProvider(@RequestBody Provider provider) {
        return providerService.updateProvider(provider);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteProvider(@PathVariable Long id) {
        return providerService.deleteProvider(id);
    }

    @PostMapping("/list")
    public Result<PageResult<Provider>> getProviderList(@RequestBody ProviderQuery query) {
        return providerService.findByPage(
                query.getCurrentPage(),
                query.getPageSize(),
                query.getProCode(),
                query.getProName()
        );
    }
}